/**************************************************************
  > File Name: main.c
  > Author: LuoLiang
  > Mail: 1204553475@qq.com 
  > Created Time: 2022年03月01日 星期二 08时28分56秒
 **************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

#define BURST 100000000LL

int data;
int loop = 1;
pthread_t tid;
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void *pthread_handler(void *args)
{
	while (1)
	{
		pthread_mutex_lock(&mut);		
		if (data < BURST)
		{
			pthread_cond_wait(&cond, &mut);
		}
		if (data >= BURST)
		{
			printf("%d\n",data);
			data = 0;
			loop = 1;
		}
		pthread_mutex_unlock(&mut);		
	}
	pthread_exit(NULL);
}

int main(int argc, char **argv)
{
	pthread_create(&tid, NULL, pthread_handler, NULL);	
	while (1)	
	while (loop)
	{
		pthread_mutex_lock(&mut);		
		data++;
		if (data >= BURST)
		{
			pthread_cond_signal(&cond);
			loop = 0;
		}
		pthread_mutex_unlock(&mut);
//		usleep(1000);		
	}
	
	pthread_join(tid, NULL);
	pthread_mutex_destroy(&mut);
	pthread_cond_destroy(&cond);
    exit(0);
}
